package com.mytijian.openapi.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.mytijian.openapi.core.client.IOpenAPIClient;
import com.mytijian.openapi.core.client.OpenAPIClient;
import com.mytijian.openapi.core.client.param.RegisterParam;
import com.mytijian.openapi.core.dao.AppConfigDAO;
import com.mytijian.openapi.core.model.AppConfig;
import com.mytijian.openapi.core.model.AppConfigDO;
import com.mytijian.openapi.core.service.OpenApiAppConfigService;

/**
 * 封装 openapi 提供完整能力
 * 
 * @author clozz
 *
 */
@Service
public class OpenApiAppConfigServiceImpl implements OpenApiAppConfigService {
	
	public static void main(String[] args) {
		IOpenAPIClient client = new OpenAPIClient();
		
		AppConfig config = new AppConfig();
		config.setBaseUrl("https://api.mytijian.com/open-api");
		config.setAppKey("apeda3646e6be449aab74044f386df1a93");
		config.setAppSecret("400ab34207444725a48aa38f3b9c5c88");
		config.setAppSignKey("78de058a931c4b4cab1984d1195cafb8");
		
//		config.setAppKey("ap4f055837045146f7b8ccb498de4a2d94");
//		config.setAppSecret("40f347a092b145dda541a7e107404490");
//		config.setAppSignKey("c59f57e281164f108fd805ad6088eb9a");
		
		String appAccessToken = "labZ8nmgCQErA4zXIzzgNPXtzyJ07Wz97fMzKOwk-f3GZzWoGPSsjnMpgDQV9I7Qjk-C08gBxCwwr0J_VPAAgOPMH8pnFUL643pt0_1BZtISEUCeXzYXvB_ea5UQVS9u51SGtGZ7sG1M4Cctu9UAV3y89kh-6k2CMbGHTa" ;//client.getAccessToken(config);
		config.setAppAccessToken(appAccessToken);
		
		RegisterParam param = new RegisterParam();
		param.setIdcard("331082199308200352");
		param.setUsername("test林知昊");
		param.setMobile("17557287637");
		
		client.register(config, param);
	}

	private Logger log = LoggerFactory.getLogger(getClass());

	/**
	 * 开放平台接口地址，暂且继续使用配置注入方式
	 */
	@Value("${openapi.baseUrl}")
	private String baseUrl;

	@Autowired
	private AppConfigDAO appConfigDAO;

	@Autowired
	private IOpenAPIClient client;

	@Override
	public void updateAccessToken(AppConfig config) {
	
		if (config==null) {
			log.warn("无法更新AccessToken。传入 AppConfig 为空。");
			return;
		}
		
		config.setBaseUrl(baseUrl);
		String token = client.getAccessToken(config);

		if (token != null) {
			
			AppConfigDO appConfig = new AppConfigDO();
			
			BeanUtils.copyProperties(config, appConfig);
			
			appConfig.setAppAccessToken(token);
			appConfig.setAppAccessTokenExpires(new Date(System.currentTimeMillis() + 7200000L));
			appConfigDAO.updateAccessToken(appConfig);
			
		} else {
			log.error("更新 AccessToken 失败 AppAlias: {}", config.getAppAlias());
		}
	}

	@Override
	public AppConfig getConfig(String appAlias) {

		AppConfigDO appConfigDO = appConfigDAO.getConfigByAlias(appAlias);
		if (appConfigDO != null) {
			AppConfig config = new AppConfig();
			BeanUtils.copyProperties(appConfigDO, config);
			config.setBaseUrl(baseUrl);
			return config;
		}
		return null;
	}

	@Override
	public List<AppConfig> listConfigs() {
		List<AppConfigDO> srcList = appConfigDAO.listAppConfigs();
		List<AppConfig> resultList = new ArrayList<>();
		if (srcList != null && !srcList.isEmpty()) {
			for (AppConfigDO appConfigDO : srcList) {
				AppConfig config = new AppConfig();
				BeanUtils.copyProperties(appConfigDO, config);
				config.setBaseUrl(baseUrl);
				resultList.add(config);
			}
			return resultList;
		}
		return new ArrayList<>();
	}

}
